from ui.constants import *
import curses

class Reader:

    def __init__(self, title: str, text: str):
        self.title = title
        self.text = text
        self.index = 0
        self.text_list = text.split("\n")

    def move_up(self):
        self.index -= 1
        if self.index < 0: self.index = 0

    def move_down(self):
        self.index += 1
        length = len(self.text_list)
        if self.index == length: self.index = length-1

    def crop_text_to_list(self, text: str, start: int, end: int):
        text_list = self.text_list
        length = len(text_list)

        if start > length: start = length - 1
        if end > length: end = length
        
        return text_list[start:end]

    def get_text_lines(self):
        if self.text: return self.text.split('\n')
        else: return list()

    def draw_text(self, screen, y, x):
        max_y, max_x = screen.getmaxyx()

        max_line_length = max_x - 2

        text_lines = self.crop_text_to_list(self.text, self.index, max_y+self.index)
        for line in text_lines:
            line_name = str(line)
            
            if len(line_name) > max_line_length:
                fline = line_name[:max_line_length]
                sline = line_name[max_line_length:]
                
                screen.addstr(y, x, fline); y += 1
                screen.addstr(y, x, sline)
            else:
                screen.addstr(y, x, line_name)
            
            y += 1

    def draw_title(self, screen):
        y, x = 0, 1 # start point
        _, max_x = screen.getmaxyx()

        curses.init_pair(5, -1, curses.COLOR_BLUE)
        bluebg = curses.color_pair(5)

        self.clear_line(screen, y, max_x-1, bluebg)

        title = self.title + (' ' * (max_x-len(self.title)-1))

        screen.addstr(y, x, title, bluebg)

        return y

    def clear_line(self, screen, y, l, attr=None):
        for i in range(l): screen.addstr(y, i, ' ', attr)

    def draw(self, screen):
        screen.erase()

        y, x = 0, 1 # start point
        
        y = self.draw_title(screen)

        curses.init_pair(1, curses.COLOR_GREEN, -1)

        self.draw_text(screen, y+1, x)

        screen.refresh()

        return y, len(self.title)+1

    def _loop(self, screen):
        while True:    
            self.draw(screen)
                
            c = screen.getch()

            if c in KEYS_UP: 
                self.move_up()
            elif c in KEYS_DOWN:
                self.move_down()
            else:
                return c


    def init_curses(self):
        # use the default colors of the terminal
        curses.use_default_colors()
        # hide the cursor
        curses.curs_set(0)

        curses.initscr()

    def _start(self, screen):
        self.init_curses()
        screen.idlok(False)
        screen.idcok(False)
        return self._loop(screen)

    def start(self):
        return curses.wrapper(self._start)

def read(title: str, text: str):
    rdr = Reader(title=title, text=text)
    
    return rdr.start()


